import loading from '@/libs/loading';
import auth from '@/service/auth';
import storage from '@/libs/storage';

export default {
  showLoading(to, from, next) {
    if (from.matched.length !== 0) {
      loading.show();
    }

    next();
  },

  auth(to, from, next) {
      if(to.name === 'login') {
          storage.remove('user');
          storage.remove('authPathList');
      }
    if (to.matched.some(record => record.meta.requiresAuth)) {
      // this route requires auth, check if logged in
      // if not, redirect to login page.
      auth.loggedIn(to).then(() => {
        next();
      }).catch(() => {
        loading.close();
        window.location.href = '/';
        // next({ path: '/login', query: { redirectUrl: to.fullPath } });
      });
    } else {
      next(); // 确保一定要调用 next()
    }
  }
};
